package com.gitee.zydaas.gateway.utils;

import java.nio.charset.StandardCharsets;

import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;

import com.alibaba.fastjson2.JSON;
import com.gitee.zydaas.common.core.result.ResultCode;
import com.gitee.zydaas.common.core.result.ResultJson;

import reactor.core.publisher.Mono;

/**
 * 异常响应工具类
 *
 * @Author kinbug
 * @Date 2021/01/29 13:30
 */
public class WebFluxUtils {

	public static Mono<Void> writeResponse(ServerHttpResponse response, ResultCode resultCode) {
		switch (resultCode) {
		case ACCESS_UNAUTHORIZED:
		case TOKEN_INVALID_OR_EXPIRED:
			response.setStatusCode(HttpStatus.UNAUTHORIZED);
			break;
		case TOKEN_ACCESS_FORBIDDEN:
			response.setStatusCode(HttpStatus.FORBIDDEN);
			break;
		default:
			response.setStatusCode(HttpStatus.BAD_REQUEST);
			break;
		}
		response.getHeaders().set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
		response.getHeaders().set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*");
		response.getHeaders().set(HttpHeaders.CACHE_CONTROL, "no-cache");
		String body = JSON.toJSONString(ResultJson.fail(resultCode));
		DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8));
		return response.writeWith(Mono.just(buffer)).doOnError(error -> DataBufferUtils.release(buffer));
	}

	private WebFluxUtils() {
		throw new IllegalStateException("Utility class");
	}
}
